﻿using System;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using EPiServer;
using EPiServer.Globalization;
using EPiServer.Web.WebControls;


namespace EPiServer.Municipality.Web.UI.PropertyControls {
    /// <summary>
    /// 
    /// </summary>
    /// <remarks></remarks>
    /// <example></example>
    public class InputGoogleMapsLocation : InputBase {

        /* *******************************************************************
        *  Properties 
        * *******************************************************************/
        #region public GoogleMapsLocation Value
        /// <summary>
        /// Get/Sets the Value of the InputGoogleMapsLocation
        /// </summary>
        /// <value></value>
        public GoogleMapsLocation Value {
            get {
                if (ValueControl == null)
                    return null;

                if (string.IsNullOrEmpty(ValueControl.Value))
                    return null;

                return GoogleMapsLocation.Parse(ValueControl.Value);
            }
            set {
                if (ValueControl == null) {
                    ValueControl = new HtmlInputHidden();
                    ValueControl.ID = "Value";
                    Controls.Add(ValueControl);
                }

                if (value == null)
                    ValueControl.Value = String.Empty;
                else
                    ValueControl.Value = value.ToString();
            }
        }
        #endregion
        #region public TextBox DisplayControl
        /// <summary>
        /// Get/Sets the DisplayControl of the InputGoogleMapsLocation
        /// </summary>
        /// <value></value>
        public TextBox DisplayControl {
            get;
            set;
        }
        #endregion
        #region public HtmlInputHidden ValueControl
        /// <summary>
        /// Get/Sets the ValueControl of the InputGoogleMapsLocation
        /// </summary>
        /// <value></value>
        public HtmlInputHidden ValueControl {
            get;
            set;
        }
        #endregion
        #region private int DialogWidth
        /// <summary>
        /// Gets the DialogWidth of the InputGoogleMapsLocation
        /// </summary>
        /// <value></value>
        private int DialogWidth {
            get { return 500; }
        }
        #endregion
        #region private int DialogHeight
        /// <summary>
        /// Gets the DialogHeight of the InputGoogleMapsLocation
        /// </summary>
        /// <value></value>
        private int DialogHeight {
            get { return 550; }
        }
        #endregion

        /* *******************************************************************
        *  Methods 
        * *******************************************************************/
        #region protected override void OnPreRender(EventArgs e)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e) {
            base.OnPreRender(e);
            if (DisplayControl != null) {
                DisplayControl.Enabled = false;
                if (Value != null)
                    DisplayControl.Text = Value.ToString();
                else
                    DisplayControl.Text = String.Empty;
            }
        }
        #endregion
        #region protected override void CreateChildControls()
        /// <summary>
        /// 
        /// </summary>
        protected override void CreateChildControls() {
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "ShowLocationSelectionDialog", CreateShowDialogScript());

            if (ValueControl == null) {
                ValueControl = new HtmlInputHidden();
                ValueControl.ID = "Value";
                Controls.Add(ValueControl);
            }
                
            DisplayControl = new TextBox();
            DisplayControl.ID = "Display";
            DisplayControl.Enabled = false;

            CopyWebAttributes(DisplayControl);
            Controls.Add(DisplayControl);

            var openDialogButton = new HtmlInputButton();
            if (!Enabled)
                openDialogButton.Attributes.Add("disabled", "disabled");

            openDialogButton.Value = "...";
            openDialogButton.Attributes.Add("class", "epismallbutton");
            openDialogButton.Attributes.Add("onclick", string.Format(
                "ShowLocationSelectionDialog('{0}',document.getElementById('{1}').value,'{2}','{3}','{4}','{5}','{6}');",
                UriSupport.ResolveUrlBySettings("~/Web/UI/PropertyControls/Dialogs/GoogleMapsLocationSelection.aspx"),
                ValueControl.ClientID, DisplayControl.ClientID, ValueControl.ClientID, ContentLanguage.PreferredCulture.Name,
                DialogWidth, DialogHeight));

            Controls.Add(openDialogButton);
        }
        #endregion
        #region private string CreateShowDialogScript()
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private string CreateShowDialogScript() {
            var dialogMethodStringBuilder = new StringBuilder();
            dialogMethodStringBuilder.AppendLine("<script type='text/javascript'>");
            dialogMethodStringBuilder.AppendLine("//<![CDATA[");
            dialogMethodStringBuilder.AppendLine("ShowLocationSelectionDialog = function(url, value, displayid, valueid, language, dialogWidth, dialogHeight, callbackMethod, callbackArguments)");
            dialogMethodStringBuilder.AppendLine("{");
            dialogMethodStringBuilder.AppendLine("  var completeUrl = url + '?value=' + value + '&displayid=' + displayid + '&valueid=' + valueid + '&epslanguage=' + language;");
            dialogMethodStringBuilder.AppendLine("  if (!callbackMethod)");
            dialogMethodStringBuilder.AppendLine("  {");
            dialogMethodStringBuilder.AppendLine("    callbackMethod = function(returnValue)");
            dialogMethodStringBuilder.AppendLine("    {");
            dialogMethodStringBuilder.AppendLine("      if (returnValue)");
            dialogMethodStringBuilder.AppendLine("      {");
            dialogMethodStringBuilder.AppendLine("         EPi.PageLeaveCheck.SetPageChanged(true);");
            dialogMethodStringBuilder.AppendLine("      }");
            dialogMethodStringBuilder.AppendLine("    }");
            dialogMethodStringBuilder.AppendLine("  }");
            dialogMethodStringBuilder.AppendLine("  if (!callbackArguments)");
            dialogMethodStringBuilder.AppendLine("  {");
            dialogMethodStringBuilder.AppendLine("    callbackArguments = null;");
            dialogMethodStringBuilder.AppendLine("  }");
            dialogMethodStringBuilder.AppendLine("  var dialogArguments = window.document;");
            dialogMethodStringBuilder.AppendLine("  var features = {width:dialogWidth, height:dialogHeight, scrollbars:'no'};");
            dialogMethodStringBuilder.AppendLine("  return EPi.CreateDialog(completeUrl, callbackMethod, callbackArguments, dialogArguments, features);");
            dialogMethodStringBuilder.AppendLine("}");
            dialogMethodStringBuilder.AppendLine("//]]>");
            dialogMethodStringBuilder.AppendLine("</script>");
            return dialogMethodStringBuilder.ToString();
        }
        #endregion
    }
}